<template>
	<view class="paidDetail">
		<nav-bars backState="1000" :bgColor="navBackground" fontColor="#fff"
			:title="info.refund_type==3 ? '退款退货详情':info.refund_type==1?'仅退款':''"></nav-bars>
		<view class="topImg">
			<image :src="baseImg+'/static/adminShop/detailBack.png'" mode=""></image>
			<!-- time -->
			<view class="time">
				<view class="title">
					<image :src="`${baseImg}/static/adminShop/detailWait.png`" mode="" v-if="info.progress_status!=2">
					</image>
					<image :src="`${baseImg}/static/adminShop/finish-icon.png`" mode="" v-else></image>
					<text v-if="info.progress_status===2 && info.refund_type==3">退款退货完成</text>
					<text v-else-if="info.progress_status===2 && info.refund_type==1">退款完成</text>
					<text v-else-if="info.refund_type==3 && info.progress_status!=2">退款退货</text>
					<text v-else-if="info.refund_type==1 && info.progress_status!=2">仅退款</text>
				</view>
			</view>
			<!-- 用户信息 -->
			<view class="userInfo" v-if="info.status==0">
				<view class="infoItem-wrap">
					<view class="title">退款信息</view>
					<view class="infoItem">
						<view class="call">
							退款金额：<text>￥{{info.refund_amount}}</text>
						</view>
					</view>
				</view>
			</view>
			<view class="userInfo" v-if="info.progress_status==2">
				<view class="infoItem-wrap">
					<view class="title">处理进度</view>
					<view class="infoItem">
						<view class="call" style="font-size: 26rpx;">
							退款成功
						</view>
					</view>
				</view>
			</view>
			<view class="userInfo" v-if="info.status==1 && info.refund_type===3 && info.progress_status!=2">
				<view class="infoItem-wrap">
					<view class="title">处理进度</view>
					<view class="infoItem">
						<view class="call" style="font-size: 26rpx;" v-if="reship.logi_no">
							买家已将商品寄回请确认查收
						</view>
						<view class="call" style="font-size: 26rpx;" v-else-if="!reship.logi_no">
							等待买家商品寄回
						</view>
					</view>
				</view>
			</view>
			<view class="userInfo" v-else-if="info.status==1 && info.refund_type===1 && info.progress_status!=2">
				<view class="infoItem-wrap">
					<view class="title">处理进度</view>
					<view class="infoItem">
						<view class="call" style="font-size: 26rpx;">
							你已同意退款
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="content" :style="'margin-top:'+navHeight+'rpx;'"
			:class="{'contentActive':order_info.order_status==3}">
			<!-- 商品清单 -->
			<view class="shopList">
				<view class="order-info">
					<view class="info-text">
						售后编号: <text>{{info.after_sale_id}}</text>
					</view>
					<view class="info-text">
						申请时间: <text>{{info.create_time}}</text>
					</view>
					<view class="order-detail">
						<view class="title">订单详情</view>
						<view class="info-text">
							下单时间: <text>{{order_info.create_time}}</text>
						</view>
						<view class="info-text">
							订单编号: <text>{{order_info.order_code}}<span
									@click="copyBtn(order_info.order_code)">复制</span></text>
						</view>
						<view class="info-text">
							订单状态: <text>{{order_info.order_status}}</text>
						</view>
					</view>
				</view>
				<view class="logistics" v-if="reship.logi_no && info.refund_type===3">
					<view class="title">客户寄回信息</view>
					<view class="row-address">
						<view class="left">
							<!-- <image :src="`${baseImg}/static/adminShop/detailAddress.png`" mode="" class="address-icon"></image> -->
							<view class="call">
								<view class="info">
									{{userInfo.name}} {{userInfo.mobile}} <span @click="toTelPhone">联系Ta</span>
								</view>
								<view class="address info-text">
									{{reship.logi_name}} {{reship.logi_no}} <span
										@click="copyBtn(reship.logi_no)">复制单号</span>
								</view>
							</view>
						</view>
					</view>
				</view>
				<h3>退款商品</h3>
				<view class="list">
					<view class="item" v-for="(item,index) in productList" :key="index">
						<view class="wrap-item">
							<image :src="item.main_imgurl" mode=""></image>
							<view class="shopInfo">
								<view class="top">
									<view class="tit omit-2">
										{{item.product_title}}
									</view>
									¥ {{item.price}}
								</view>
								<view class="spec">
									<view class="specItem">
										<span v-for="(item1,index1) in item.attr_params"
											:key="index1">{{item1.value}}</span>
									</view>
									<span>x{{item.refund_num}}</span>
								</view>
							</view>
						</view>
						<view class="refund-list-box">
							<view class="item-title">
								退款原因：<text>{{item.refund_msg}}</text>
							</view>
							<view class="row-wrap">
								<view class="refund_img" v-for="(items,index) in item.refund_img"
									@click="preview(index,item.refund_img)">
									<image :src="items.image_path"></image>
								</view>
							</view>
							<view class="item-title">
								描述：<text>{{item.refund_desc}}</text>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 按钮 -->
		<view class="btn">
			<view class="btn_item" v-if="info.status===0" @click="refuseModal(info.after_sale_id)">拒绝退款申请</view>
			<view class="btn_item consent" @click="agreeModal(info.refund_type,info.after_sale_id)"
				v-if="info.status===0 && info.refund_type===1">同意退款申请</view>
			<view class="btn_item consent" @click="agreeModal(info.refund_type,info.after_sale_id)"
				v-if="info.status===0 && info.refund_type===3">同意退款退货申请</view>
			<view class="btn_item consent" @click="refundOrder" v-if="info.status===1 && info.progress_status!=2">确定退款
			</view>
		</view>
	</view>
</template>

<script>
	import after_sale from '@/service/aftersale.js'
	export default {
		data() {
			return {
				navBackground: '',
				baseImg: this.$static,
				navHeight: 200,
				after_sale_id: '', //售后订单id
				order_info: {},
				userInfo: {},
				address_all: '',
				h: 23, //时
				m: 59, //分
				s: 59, //秒
				countdown: '24:00:00', //倒计时
				timer: null, //定时器
				productList: [],
				info: {},
				remark: '', //拒绝退款说明
				reship: {}, //退货信息
			}
		},
		onLoad(options) {
			this.navHeight = 490 - (Number(this.$store.state.system.navBarHeight) * 2)
			this.after_sale_id = options.after_sale_id
			this.getOrderDetail()
		},
		methods: {
			// 获取订单详情
			getOrderDetail() {
				let params = {
					store_id: this.$store.state.index.sid,
					after_sale_id: this.after_sale_id
				}
				after_sale.getOrderDetail(params).then(res => {
					console.log(res, '订单详情')
					if (res.code == 1) {
						this.order_info = res.data.orderInfo ? res.data.orderInfo : {};
						if (res.data.orderInfo.order_status == 2) {
							this.order_info.order_status = '待发货'
						} else if (res.data.orderInfo.order_status == 3) {
							this.order_info.order_status = '待收货'
						} else if (res.data.orderInfo.order_status == 5) {
							this.order_info.order_status = '已关闭'
						}
						let data=res.data;
						this.userInfo = data.userInfo;
						this.productList = data.refundItems;
						this.info = data;
						this.address_all = data.userInfo.province + data.userInfo.city + data.userInfo
							.area + data.userInfo.address;
						this.reship = data.reship;
						
						if(data.status===1 && data.progress_status!=2 && data.refund_type===1){
							this.refundBusy(data.refund_id);
						}
					}
				})
			},
			//退款
			refundOrder() {
				uni.showModal({
					content: '是否确定同意退款？',
					success: res => {
						if (res.confirm) {
							let params = {
								store_id: this.$store.state.index.sid,
								refund_id: this.info.refund_id
							}
							after_sale.aftersaleRefund(params).then(res => {
								if (res.code == 1) {
									uni.showToast({
										title: '退款成功',
										icon: 'success'
									})
									this.getOrderDetail();
								} else {
									uni.showToast({
										title: res.msg,
										icon: 'none'
									})
								}
							})
						}
					}
				})
			},

			// 复制按钮
			copyBtn(e) {
				uni.setClipboardData({
					data: e,
					success: function() {
						console.log('复制成功')
					}
				});
			},

			// 联系打电话
			toTelPhone() {
				console.log(this.userInfo.mobile, '电话电话')
				uni.makePhoneCall({
					// 手机号
					phoneNumber: this.order_info.mobile,
					// 成功回调
					success: (res) => {
						console.log('调用成功!')
					},
					// 失败回调
					fail: (res) => {
						console.log('调用失败!')
					}
				});
			},
			//拒绝审核
			refuseModal(after_sale_id) {
				var _this = this;
				uni.showModal({
					title: '提示',
					placeholderText: '请填写拒绝用户申请退款说明',
					editable: true,
					success: function(res) {
						if (res.confirm) {
							if (res.content == '' || !res.content) {
								uni.showToast({
									title: '请填写拒绝说明10-20字',
									icon: 'error'
								})
								_this.refuseModal(after_sale_id)
							} else {
								_this.remark = res.content;
								_this.toCloseOrder(2, after_sale_id);
							}
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			//同意审核
			agreeModal(e, after_sale_id) {
				var content = '';
				var _this = this;
				if (e == 1) {
					content = '是否同意用户退款申请'
				}
				if (e == 3) {
					content = '是否同意用户退款退货申请'
				}
				uni.showModal({
					title: '提示',
					content: content,
					success: function(res) {
						if (res.confirm) {
							_this.toCloseOrder(1, after_sale_id);
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			//退款/退货
			toCloseOrder(type, after_sale_id) {
				let params = {
					after_sale_id: after_sale_id, //申请单号
					status: type,
					remark: this.remark, //备注
					store_id: this.$store.state.index.sid,
				}
				after_sale.aftersaleAudit(params).then(res => {
					if (res.code == 1) {
						let info = this.info;
						if (info.refund_type != 1) {
							uni.showToast({
								title: res.msg,
								icon: 'success'
							})
						}
						this.getOrderDetail();
					} else {
						uni.showToast({
							title: res.msg,
						})
					}
				})
			},
			//无感退款
			refundBusy(refund_id) {
				let params = {
					store_id: this.$store.state.index.sid,
					refund_id: refund_id
				}
				after_sale.aftersaleRefund(params).then(res => {
					if (res.code == 1) {
						uni.showToast({
							title: '退款成功',
							icon: 'success'
						})
						this.getOrderDetail();
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				})
			},
			preview(index, urls) {
				let img_url = [];
				for (let i = 0; i < urls.length; i++) {
					img_url.push(urls[i].image_path);
				}
				uni.previewImage({
					urls: img_url,
					current: index,
					success: function(data) {
						console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
					},
					fail: function(err) {
						console.log(err.errMsg);
					}
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.paidDetail {
		width: 100%;

		.topNav {
			position: relative;
			z-index: 100;

		}

		.topImg {
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			height: 468rpx;

			image {
				width: 100%;
				height: 100%;
			}

			.time {
				padding: 0 44rpx;
				position: absolute;
				bottom: 190rpx;
				left: 0;
				color: #fff;

				.title {
					display: flex;
					align-items: center;
					margin-bottom: 8rpx;

					image {
						margin-right: 16rpx;
						width: 40rpx;
						height: 40rpx;
						border-radius: 50%;
					}

					font: 500 36rpx/36rpx PingFang SC;
				}

				.onlyTime {
					font: 400 22rpx/36rpx PingFang SC;
				}
			}

			// 用户信息
			.userInfo {
				width: 100%;
				position: absolute;
				left: 0;
				bottom: -22rpx;

				.infoItem-wrap {
					padding: 28rpx 24rpx;
					margin: 0 auto;
					width: 702rpx;
					height: 142rpx;
					background-color: #fff;
					border-radius: 16rpx;

					.title {
						font-size: 29rpx;
						font-family: PingFang SC;
						font-weight: bold;
						color: #252525;
					}

					.infoItem {
						display: flex;
						justify-content: space-between;

						image {
							width: 40rpx;
							height: 40rpx;
						}

						.call {
							display: flex;
							align-items: center;
							padding-top: 20rpx;
							color: #9B9B9B;

							text {
								color: #A88E5A;
								text-indent: 4rpx;
							}
						}

					}
				}

			}
		}

		// 内容
		.content {
			padding: 12rpx 24rpx 0;
			margin-bottom: 116rpx;
			padding-bottom: constant(safe-area-inset-bottom);
			padding-bottom: env(safe-area-inset-bottom);

			// 商品清单
			.shopList {
				background-color: #fff;
				padding: 0 28rpx 28rpx 28rpx;
				border-radius: 16rpx;

				h3 {
					padding-bottom: 28rpx;
					font: 500 30rpx/40rpx PingFang SC;
					color: #0A0605;
				}

				.order-info {
					.info-text {
						display: flex;
						color: #9B9B9B;
						height: 58rpx;
						line-height: 58rpx;

						text {
							height: 58rpx;
							line-height: 58rpx;
							color: #3D3D3D;
							text-indent: 22rpx;
						}

						span {
							margin-left: 18rpx;
							padding: 0 14rpx;
							border-radius: 18rpx;
							color: #DBA954;
							font: 400 18rpx/40rpx PingFang SC;
							background-color: rgba(219, 169, 84, 0.1);
						}
					}

					.order-detail {
						padding-top: 50rpx;

						.title {
							margin-bottom: 26rpx;
							font-size: 29rpx;
							font-family: PingFang SC;
							font-weight: bold;
							color: #252525;
						}

					}
				}

				.logistics {
					padding-top: 50rpx;

					.title {
						font-size: 29rpx;
						font-family: PingFang SC;
						font-weight: bold;
						color: #252525;
					}

					.row-address {
						padding: 40rpx 24rpx 24rpx 0;
						display: flex;
						justify-content: space-between;
						margin: 0 auto;
						width: 702rpx;
						height: 188rpx;
						background-color: #fff;
						border-radius: 16rpx;

						image {
							width: 40rpx;
							height: 40rpx;
						}

						.left {
							display: flex;

							.address-icon {
								margin-top: 20rpx;
							}

							.call {
								margin: 0 20rpx 0 0;
								display: flex;
								flex-direction: column;

								.info {
									margin-bottom: 20rpx;
									display: flex;
									color: #0A0605;
									font: 500 26rpx/40rpx PingFang SC;

									span {
										margin-left: 16rpx;
										padding: 0rpx 12rpx;
										border-radius: 18rpx;
										background-color: #F5F6F6;
										color: #A88E5A;
										font: 400 26rpx/40rpx PingFang SC;
									}
								}

								.address {
									color: #505050;
									font: 400 26rpx/40rpx PingFang SC;
								}
							}
						}

						.info-text {
							display: flex;
							color: #9B9B9B;
							height: 58rpx;
							line-height: 58rpx;

							text {
								height: 58rpx;
								line-height: 58rpx;
								color: #3D3D3D;
								text-indent: 22rpx;
							}

							span {
								margin-left: 18rpx;
								padding: 0 14rpx;
								border-radius: 18rpx;
								color: #DBA954;
								background-color: rgba(219, 169, 84, 0.1);
							}
						}
					}
				}

				.list {
					padding-bottom: 100rpx;

					.item {
						margin-bottom: 32rpx;

						.wrap-item {
							display: flex;
						}

						image {
							width: 148rpx;
							height: 148rpx;
							border-radius: 8rpx;
						}

						.shopInfo {
							width: 478rpx;
							margin-left: 24rpx;
							display: flex;
							flex-direction: column;

							.top {
								color: #505050;
								font: 400 26rpx/40rpx PingFang SC;

								.tit {
									width: 328rpx;
								}

								display: flex;
								justify-content: space-between;
							}

							.spec {
								display: flex;
								justify-content: space-between;
								margin-top: 40rpx;
								color: #A0A0A0;
								font: 400 22rpx/22rpx PingFang SC;
							}
						}

						.refund-list-box {
							margin-top: 24rpx;

							.item-title {
								font-size: 24rpx;
								font-family: PingFang SC;
								font-weight: 500;
								height: 50rpx;
								line-height: 50rpx;
								color: #3D3D3D;
							}

							.row-wrap {
								display: flex;
							}

							.refund_img {
								image {
									width: 150rpx;
									height: 150rpx;
									margin: 10rpx;
								}
							}
						}
					}
				}

				// 价钱
				.price {
					padding-top: 32rpx;
					border-top: 2rpx solid #F5F5F5;

					view {
						display: flex;
						justify-content: space-between;
					}

					.all {
						font: 400 26rpx/40rpx PingFang SC;
						color: #505050;
					}

					.need {
						font: 500 26rpx/40rpx PingFang SC;
						color: #0A0605;

						span {
							color: #DBA954;
						}
					}
				}
			}

			// 订单信息
			.orderInfo {
				margin-top: 0rpx;
				background-color: #fff;
				padding: 28rpx 28rpx 48rpx 28rpx;
				border-radius: 16rpx;

				h3 {
					font: 500 30rpx/40rpx PingFang SC;
					color: #0A0605;
				}

				.top {
					padding: 28rpx 0 0;
					margin-bottom: 24rpx;
					border-bottom: 2rpx solid #f5f5f5;

					.item {
						font: 400 26rpx/40rpx PingFang SC;
						color: #A0A0A0;
						display: flex;
						margin-bottom: 24rpx;
						align-items: center;

						.txt {
							display: flex;
							color: #505050;
							margin-left: 32rpx;

							span {
								margin-left: 18rpx;
								padding: 0 14rpx;
								border-radius: 18rpx;
								color: #DBA954;
								font: 400 18rpx/40rpx PingFang SC;
								background-color: rgba(219, 169, 84, 0.1);
							}
						}
					}
				}

				.bottom {
					font: 400 26rpx/40rpx PingFang SC;
					color: #A0A0A0;
					display: flex;
					align-items: center;

					span {
						margin-left: 32rpx;
						color: #505050;
					}
				}
			}
		}

		.contentActive {
			padding-top: 30rpx;
		}

		// 按钮
		.btn {
			width: 100%;
			height: 92rpx;
			background-color: #fff;
			position: fixed;
			bottom: 0;
			left: 0;
			display: flex;
			justify-content: flex-end;
			padding: 0 24rpx;
			height: calc(92rpx+ constant(safe-area-inset-bottom));
			height: calc(92rpx + env(safe-area-inset-bottom));
			padding-bottom: constant(safe-area-inset-bottom);
			padding-bottom: env(safe-area-inset-bottom);

			.btn_item {
				width: 204rpx;
				height: 56rpx;
				border-radius: 28rpx;
				border: 2rpx solid #505050;
				font: 400 24rpx/56rpx PingFang SC;
				color: #505050;
				text-align: center;
				margin: 18rpx 0 18rpx 20rpx;
			}

			.consent {
				color: #C3AB7C;
				border: 1px solid #C3AB7C;
				border-radius: 28px;
			}
		}
	}
</style>
